package cc.facesign.admin.utils;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.ArrayList;
import java.util.List;

/**
 * 分页工具类
 */

public class PageUtils {
    /**
     * 对list对象分页
     * @param data 要分页的list集合
     * @param current 当前页
     * @param size 每页数量
     * @return IPage
     */
    public static <T> IPage<T> page(List<T> data, int current, int size) {
        IPage<T> page = new Page<>(current, size);
        int count = data.size();
        List<T> pageList = new ArrayList<>();
        int currId = current > 1 ? (current - 1) * size : 0;
        for (int i = 0; i < size && i < count - currId; i++) {
            pageList.add(data.get(currId + i));
        }
        page.setSize(size);
        page.setCurrent(current);
        page.setTotal(count);
        page.setPages(count % 10 == 0 ? count / 10 : count / 10 + 1);
        page.setRecords(pageList);

        return page;
    }
}
